﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CustomControls;
using System.IO;

namespace UIwpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class wndMain : CustomWindow
    {
        public wndMain()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
#if RELEASE
            if (ValidateSoftware())
            {
                DateTime InstalledTime = Properties.Settings.Default.InstalledDateTime;
                TimeSpan ts = DateTime.Now.Subtract(InstalledTime);
                if (ts.TotalSeconds < 15552000)
                //if (ts.TotalSeconds < 3600)
                {
                    //Application.Run(new frmOnlineUI2());
                    InitializeComponent();
                }
                else
                {
                    MessageBox.Show(
                            "Your software has expired.\nPlease contact CNA Software to purchase new key.",
                            "Error",
                            MessageBoxButton.OK,
                            MessageBoxImage.Exclamation,
                            MessageBoxResult.OK);
                    //Application.Run(new frmRegister());
                    (new SubForms.wndRegister()).ShowDialog();
                    CloseOnInit(); // More code from Dien Vu
                }
            }
            else
            {
                MessageBox.Show(
                        "Your software is not licienced.\nPlease contact CNA Software to purchase new key.",
                        "Error",
                        MessageBoxButton.OK,
                        MessageBoxImage.Exclamation,
                        MessageBoxResult.OK);
                //Application.Run(new frmRegister());
                (new SubForms.wndRegister()).ShowDialog();
                CloseOnInit(); // More code from Dien Vu
            }
#else
            InitializeComponent();
#endif
        }

        /// <summary>
        /// Have to call return after call this method to avoid add duplicate close again & again.
        /// </summary>
        private void CloseOnInit()
        {
            this.Loaded += (o, e) => { this.Close(); e.Handled = true; }; // Handled = true will not call any other Loaded anymore.
            return;
        }


        #region For License Key
        static string GenerateHashCode(string privateKey, string serialKey)
        {
            System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
            byte[] preHash = System.Text.Encoding.UTF32.GetBytes(privateKey + serialKey);
            byte[] hash = sha.ComputeHash(preHash);
            return System.Convert.ToBase64String(hash, 0, 15);
        }

        static bool ValidateSoftware()
        {
            string PrivateKey;
            string SerialKey;

            string KeyFile = @"C:\Temp\Key.txt";
            string PrivateKeyFile = @"C:\Temp\PrivateKey.txt";
            string HashCodeFile = @"C:\Temp\HashCodes.txt";

            try
            {
                using (StreamReader s = new StreamReader(PrivateKeyFile))
                {
                    PrivateKey = s.ReadLine();
                }
                using (StreamReader s = new StreamReader(KeyFile))
                {
                    SerialKey = s.ReadLine();
                }
                string hashCode = GenerateHashCode(PrivateKey, SerialKey);
                using (StreamReader sr = new StreamReader(HashCodeFile))
                {
                    string allHashCodes = sr.ReadToEnd();
                    if (allHashCodes.Contains(hashCode))
                    {
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return false;
        }
        #endregion

        private void CustomWindow_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            
        }
    }
}
